
//THIS CODE FILE BUILDS THE STACKED MINIMUM WAGE FILE NEEDED FOR THE EVENT STUDY
	//IT ALSO PRODUCES FIGURE A.1, FIGURE A.2, FIGURE A.3, AND FIGURE A.4

global main "[YOUR PATH HERE]"
global output "${main}/output"
	
//figure switches
local switch_map       = 0
local switch_timeline  = 0
local switch_mw_paths  = 0
local switch_histogram = 0

// one of these switches must be on!!
	//indicates whether we want to use percent change as our measure of MW change size 
	//or whether we want to use a nominal change (50 cent, 75 cents, etc.)
local switch_nominal   = 0
local switch_pctchng   = 1
local switch_pctHSwage = 0

if `switch_nominal'{
	local dmw_threshold = .5
		if `dmw_threshold' >= 1{
			global dmwlevel = "`dmw_threshold'"
		}
		else{
			global dmwlevel = "`=`dmw_threshold'*100'"
		}
	local xtitle "minimum wage change"
	local width 0.125
}
else if `switch_pctchng'{
	local dmw_threshold = .08
		global dmwlevel = "`=`dmw_threshold'*100'pct" //"10pct" //  // //
	local xtitle "percent change to minimum wage"
	local width 0.01
}

********************************************************************************
**MINIMUM WAGE CHANGES**
********************************************************************************
//start with the state-level changes
use ${data}/mw/exports/mw_state_changes.dta, clear
//drop irrelevant vars
	drop mw_* source*
	drop if year < 1986 | year > 2020
	drop if statefips == 11
	drop date day
	
//there is one instance in which there are two changes in the same month and year (Montana, 1991), we combine here
collapse (max) mw (first) stateabb, by(year month statefips)

//generate an event number
gen event = _n 

//create two copies of each observation so we can merge in wages 
expand 2 , generate(dup)
	bysort event (dup): replace month = month-1 if dup
	//replace those year and months that are equal to 0 now because the change happened in Jan
		replace year = year-1 if month == 0
		replace month = 12 if month == 0
		
tempfile changes
	save `changes'
	
//now need to grab the month and year of the minimum wages out of the minwage dataset
use ${data}/mw/exports/mw_state_monthly.dta, clear

//separate monthly date out into month and year variables
gen year = yofd(dofm(monthly_date))
gen month = month(dofm(monthly_date))
	drop monthly_date
	
merge 1:m year month statefips using `changes', assert(match master) keep(match) nogen
	replace dup = 2 if dup == 0

//now we have two obs for each event in the month they happened and the month befroe they happened
//generate the size of the change variable by taking the difference between the minimum wage in these two months
bysort event (dup) : gen dmw = max_mw - max_mw[_n-1]
	keep if !missing(dmw) //this drops the expanded observations from the year before the change

//create an academic year variable
gen year_academic = 0
forval yr = 1979/2021{
		replace year_academic = `yr' if month > 6  & year == `yr'
		replace year_academic = `yr' if month <= 6 & year == `=`yr'+1'
}

assert year_academic > 0
	drop if year_academic == 1979 | year_academic == 2021
	
//now we want to start dealing with changes at the academic year level so we combine changes in the same year same state 
collapse (firstnm) stateabb (sum) dmw (max) mw, by(statefips year_academic)
	drop if dmw <= 0 //there are a few rogue minwage changes that we drop here
	
//generate what we need to identify variables of interest based on the locals at the top of the file
if `switch_pctchng'{
	gen mw_pctchng = dmw/mw
		replace dmw = mw_pctchng
		drop mw*
}
if `switch_pctHSwage'{
	merge 1:1 statefips year_academic using `HS_wages', keep(match) nogen
	gen mw_pctchngHS = dmw/uhe
		replace dmw = mw_pctchng
		drop mw* uhe
}

//create indicators for whether these are events of interest or smaller minimum wage changes
gen dmw_interest = (dmw >= `dmw_threshold')
gen dmw_small = (dmw < `dmw_threshold')
	
***********************************
***SUMM STAT FIGURES***
***********************************
//histogram of minimum wage changes	
if `switch_histogram'{
	twoway (histogram dmw if dmw< `dmw_threshold', fcol(maroon%40) lcol(maroon) freq width(`width')) ///
		   (histogram dmw if dmw>= `dmw_threshold', fcol(dknavy%60) lcol(dknavy) freq width(`width')), ///
				legend(off) xline(`dmw_threshold', lpatt(dash) lcolor(gs8)) xtitle("`xtitle'") ytitle("")
		graph export "${main}/output/jat/descriptive/hist_minwage_changes${dmwlevel}.pdf", replace
}
	

//map of minimum wage changes

if `switch_map'{
	preserve
		gen count = 1
			collapse (sum) count, by(statefips)
		maptile count, geo(state) geoid(statefips) fcolor(Blues)
			graph export "${main}/output/jat/descriptive/mwchange_map_total.pdf", replace
	restore
	preserve
		gen count = 1
		keep if dmw_interest
			collapse (sum) count, by(statefips)
		maptile count, geo(state) geoid(statefips) fcolor(Blues)
			graph export "${main}/output/jat/descriptive/mwchange_map${dmwlevel}_interest.pdf", replace
	restore
}


//timeline of minimum wage changes
if `switch_timeline'{
	preserve
		labmask statefips, values(stateabb)
		grstyle init
		grstyle set plain
		grstyle set size 8pt: subheading axis_title
		twoway (scatter statefips year_academic if dmw_interest, msym(O) msize(small) mlcolor(dknavy)  mfcolor(dknavy%90) mlab(stateabb) ///
						mlabpos(4) mlabcol(black) mlabsize(tiny))  ///
			   (scatter statefips year_academic if dmw_small   , msym(S) msize(vsmall) mlcolor(maroon) mfcolor(maroon%60) mlab(stateabb) ///
						mlabpos(4)mlabcol(black) mlabsize(tiny)), ///
			   xline(1990 1991 1996 1997 2007 2008 2009, lcolor(gs8)) ylab(none) xtitle("") ytitle("") xlab(1986(4)2020, labsize(small)) ///
			   legend(off)
			graph export "${main}/output/jat/descriptive/mwchange_timeline_acyr${dmwlevel}.pdf", replace
	restore
}

//renumber events
gsort -dmw_interest
gen event = _n if dmw_interest
	summ event 
		local numevents = r(max)

//save a version that keeps all events
preserve
	drop event
	tempfile allchanges
		save `allchanges'
restore

//save a version that only keeps the major events
preserve
	keep if dmw_interest 
		rename year_academic dmw_ay
		rename statefips  dmw_statefips
		rename stateabb  dmw_statename
	tempfile events
		save `events'
restore

********************************************************************************
**MINIMUM WAGE MONTHLY DATA**
********************************************************************************
//prep minwage datasets
use ${data}/mw/exports/mw_state_monthly.dta, clear

//separate monthly date out into month and year variables
gen year = yofd(dofm(monthly_date))
gen month = month(dofm(monthly_date))
	
//keep years that are relevant given our enrollment data
keep if year < 2021 & year > 1979

//create an academic year variable
gen year_academic = 0
forval yr = 1979/2021{
		replace year_academic = `yr' if month > 6  & year == `yr'
		replace year_academic = `yr' if month <= 6 & year == `=`yr'+1'
}

assert year_academic > 0
	drop if year_academic == 1979 | year_academic == 2021
	
//merge in federal change dates
merge m:1 month year using "${data}/mw/exports/mw_federal_changes.dta", keep(match master) //drops changes that are outside of our panel
	gen dmw_fed = (_merge == 3)
		drop _merge day date fed_mw source
levelsof monthly_date if dmw_fed, local(fedchanges)

//plot this to show min wage changes over time
if `switch_mw_paths'{
	xtset statefips monthly_date 
	colorpalette spmap rainbow, nograph
	xtline max_mw, overlay legend(off) tlabel(1985m1(60)2020m1, format("%tmCCYY")) xtitle("") ytitle("state maximum minimum wage") ///
		xline(`fedchanges', lpatt(dash) lcolor(gs8))
	graph export "${main}/output/jat/descriptive/mw_paths${dmwlevel}.pdf", replace
}

//collapse to get these at the state by year level instead of monthly 
collapse (max) min* mean* max* (sum) dmw_fed (firstnm) stateabb, by(statefips year_academic)

//merge in small and significant change indicators 
merge 1:1 year_academic statefips using `allchanges', keep(match master) //drops changes that are outside of our panel
	drop _merge
	replace dmw = 0 if missing(dmw)
	replace dmw_interest = 0 if missing(dmw_interest)
	replace dmw_small = 0 if missing(dmw_small)
	
save "${main}/data/mw_changes.dta", replace

//create a never treated indicator for states that never have a change of interest
bysort statefips: egen t = total(dmw_interest)
	gen never_treat = (t == 0)
	drop t

********************************************************************************
**CREATE STACKED DATASET**
********************************************************************************
	
//now we have a dataset at the state x ACADEMIC year level with indicators for small, significant, and federal changes
//merge one copy of this dataset onto each event so that each event has its own dataset 
forval e = 1/`numevents'{
	preserve
		gen event = `e'
		merge m:1 event using `events', keep(match) nogen
		tempfile event`e'
			save `event`e''
	restore
}
forval x = 2/`numevents'{
	use `event1', clear
		append using `event`x''
	tempfile event1
		save `event1', replace
}

***********************************
***CREATE LEADS AND LAGS***
***********************************

bysort event (year_academic statefips): gen rel_time = year_academic - dmw_ay
		
summ rel_time
	local relmin = abs(r(min))
	local relmax = abs(r(max))
	
gen treated = (statefips == dmw_statefips)

// leads
cap drop F_*
forval x = 1/`relmin' {  // drop the first lead
	gen     F_`x' = rel_time == -`x'
	replace F_`x' = 0 if never_treat==1
	gen     F`x'treat = F_`x'*treated
}
	
//lags
cap drop L_*
forval x = 0/`relmax' {
	gen     L_`x' = rel_time ==  `x'
	replace L_`x' = 0 if never_treat==1
	gen     L`x'treat = L_`x'*treated
}


***********************************
***CREATE CONTROLS***
***********************************

//controls should be equal to one if these states are acting as a control state in the event, but have an event themselves in the post period
gen control = (statefips != dmw_statefips)
bysort event (year_academic) : gen post = (rel_time >= 0)

gen control_origeventpost = dmw_interest * control * post
gen control_fedeventpost = dmw_fed * control * post
gen control_othereventpost = dmw_small * control * post


save "${main}/data/mw_stacked.dta", replace
